package com.xcgame.app;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.xcgame.dlc.DiskLruCacheUtil;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {

    DiskLruCacheUtil cacheImage;
    DiskLruCacheUtil cacheText;
    ImageView iv_show;
    TextView tv_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_show = (ImageView) findViewById(R.id.iv_bitmap);
        tv_text = (TextView) findViewById(R.id.tv_text);

        //字符串类型缓存示例
        cacheText = new DiskLruCacheUtil(this,"text");
        cacheText.write("test",tv_text.getText().toString().getBytes());
        String ss = "读取："+cacheText.readOfText("test");
        tv_text.setText(ss);

        //Bitmap类型缓存示例
        cacheImage = new DiskLruCacheUtil(this,"image");
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
        cacheImage.write("test",baos.toByteArray());
        Bitmap newBitmap = cacheImage.readOfBitmap("test");
        iv_show.setImageBitmap(newBitmap);
    }
}
